281D - Maximum Xor Secondary - CodeForces Solution


two pointers *1800

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <bits/stdc++.h>
#define IOS                  \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define fast_io std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define MOD 1000000007
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<vector<long long>> vvll;
typedef vector<long long> vll;
typedef vector<string> vs;

int main()
{
    // IOS;
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif
    ll n;
    cin>>n;
    vll a(n);

    for(int i=0;i<n;i++)
    cin>>a[i];

    vll ngr(n), ngl(n);

    stack<ll> st;

    for(int i=0;i<n;i++){
        while(!st.empty() && a[st.top()]<=a[i]){
            st.pop();
        }

        if(st.empty()){
            ngl[i] = i;
        }
        else{
            ngl[i] = st.top();
        }

        st.push(i);
    }

    st = stack<ll>();

    for(int i=n-1;i>=0;i--){
        while(!st.empty() && a[st.top()]<=a[i]){
            st.pop();
        }

        if(st.empty()){
            ngr[i] = i;
        }
        else{
            ngr[i] = st.top();
        }

        st.push(i);
    }

    ll res = 0;

    for(int i=0;i<n;i++){
        ll temp = max(a[i]^a[ngl[i]], a[i]^a[ngr[i]]);
        res = max(res, temp);
    }

    cout<<res;
}


Comments

Submit
0 Comments
More Questions

454B - Little Pony and Sort by Shift
1152A - Neko Finds Grapes
1719B - Mathematical Circus
1719C - Fighting Tournament
1642A - Hard Way
285C - Building Permutation
1719E - Fibonacci Strings
1696C - Fishingprince Plays With Array
1085A - Right-Left Cipher
1508B - Almost Sorted
1690C - Restoring the Duration of Tasks
1055A - Metro
1036D - Vasya and Arrays
1139C - Edgy Trees
37A - Towers
353A - Domino
409H - A + B Strikes Back
1262A - Math Problem
158C - Cd and pwd commands
194A - Exams
1673B - A Perfectly Balanced String
1104B - Game with string
1169B - Pairs
1567D - Expression Evaluation Error
78A - Haiku
1287A - Angry Students
1428A - Box is Pull
234B - Reading
581B - Luxurious Houses
1481C - Fence Painting